package com.penghk.springbootdemo;

import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ReadWriteMap<K , V> {

    private final Map<K , V> map;

    private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
    private final Lock readLock = readWriteLock.readLock();
    private final Lock writeLock = readWriteLock.writeLock();



    public ReadWriteMap(Map<K, V> map) {
        this.map = map;
    }

    public V put(K key , V value) {
        writeLock.lock();
        try {
            return map.put(key , value);
        }finally {
            writeLock.unlock();
        }
    }

    public V get(K key) {
        readLock.lock();
        try {
            return map.get(key);
        }finally {
            readLock.unlock();
        }
    }


}
